package com.my.app.tools.core.business;

import com.google.common.collect.Lists;
import com.my.app.tools.core.service.common.AppConfigService;
import com.my.app.tools.core.service.common.ImageService;
import com.my.app.tools.pojo.AppConstants;
import com.my.app.tools.pojo.em.cache.OcrService;
import com.my.app.tools.pojo.em.cache.OtherService;
import com.my.app.tools.pojo.em.cache.TranslateService;
import com.my.app.tools.pojo.em.client.OcrClientBaiDu;
import com.my.app.tools.pojo.em.client.OcrClientTencent;
import com.my.app.tools.pojo.vo.*;

import java.awt.image.BufferedImage;
import java.util.List;

public class CoreBusiness {

    /**
     * ocr文字识别服务类型
     *
     * @return
     * @author guochaohui
     * @date 2020-04-11 15:09
     */
    public static OcrService[] ocrServiceType() {
        return OcrService.values();
    }

    /**
     * ocr文字识别
     *
     * @param image
     * @param ocrService
     * @return
     * @author guochaohui
     * @date 2020-04-03 16:27
     */
    public static List<String> doImageOcr(BufferedImage image, OcrService ocrService) throws Exception {
        byte[] imageData = ImageService.imageToBytes(image);
        if (imageData.length > AppConstants.IMAGE_MAX_SIZE) {
            imageData = ImageService.compressImage(image, 1, AppConstants.IMAGE_MAX_SIZE / imageData.length);
        }
        GlobalResponseEntity<List<String>> response = ocrService.getService().imageOcr(imageData);
        if (response.ok()) {
            return response.getData();
        } else {
            return Lists.newArrayList(errorMsg(response.getMsg()));
        }
    }

    /**
     * 翻译服务类型
     *
     * @return
     * @author guochaohui
     * @date 2020-04-11 15:10
     */
    public static TranslateService[] translateServiceType() {
        return TranslateService.values();
    }

    /**
     * 翻译语言
     *
     * @param translateService
     * @return
     * @author guochaohui
     * @date 2020-04-11 16:09
     */
    public static TranslateLangVO[] translateLang(TranslateService translateService) {
        GlobalResponseEntity<TranslateLangVO[]> response = translateService.getService().translateLang();
        if (response.ok()) {
            return response.getData();
        } else {
            return new TranslateLangVO[]{};
        }
    }

    /**
     * 翻译
     *
     * @param str
     * @param fromLang
     * @param toLang
     * @param translateService
     * @return
     * @author guochaohui
     * @date 2020-04-03 16:27
     */
    public static List<String> doTranslate(String str, String fromLang, String toLang, TranslateService translateService) {
        GlobalResponseEntity<List<String>> response = translateService.getService().translate(str, fromLang, toLang);
        if (response.ok()) {
            return response.getData();
        } else {
            return Lists.newArrayList(errorMsg(response.getMsg()));
        }
    }

    /**
     * 银行类型
     *
     * @return
     * @author guochaohui
     * @date 2020-04-07 14:31
     */
    public static Object[] bankTypes() {
        List<String> list = OtherService.INSTANCE.getMakePeopleInfoService().queryBankTypes();
        list.add(0, "任意银行");
        return list.toArray();
    }

    /**
     * 生成随机个人信息
     *
     * @param generateCount
     * @param gender
     * @param bankType
     * @return
     * @author guochaohui
     * @date 2020-04-07 14:38
     */
    public static List<PeopleInfoVO> generatePeopleInfo(Integer generateCount, Integer gender, Integer bankType) {
        return OtherService.INSTANCE.getMakePeopleInfoService().generatePeopleInfo(new MakePeopleInfoParamVO(generateCount, gender, bankType));
    }

    /**
     * 抓取企业信息
     *
     * @param keyword
     * @param maxCount
     * @return
     * @author guochaohui
     * @date 2020-04-07 18:28
     */
    public static List<EnterpriseInfoVO> spiderEnterpriseInfo(String keyword, int maxCount) {
        return OtherService.INSTANCE.getSpiderEnterpriseInfoService().queryCreditWithShanDong(keyword, maxCount);
    }

    /**
     * 重新加载配置
     *
     * @return
     * @author guochaohui
     * @date 2020-04-08 14:23
     */
    public static void reloadAppConfig() {
        AppConfigService.reloadConfig();
        OtherService.INSTANCE.reloadConfig();
        OcrClientBaiDu.INSTANCE.initClient();
        OcrClientTencent.INSTANCE.initClient();
        TranslateService.reloadConfig();
    }

    private static String errorMsg(String msg) {
        return "---- ".concat(msg).concat(" ----");
    }

}
